package br.com.buscapecas.model.cursor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collection;



/**
 * @author Maikon Rafael da Rocha
 * @since 26/08/2010 07:46:31
 */
public class QueryResult<T extends Collection<?>> implements Serializable {

    /**
     * Atributo <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = -2376050883405622937L;
    private T resultList;
    private Integer resultRows;
    private Integer resultPages;
    private Integer currentPage;
    private Integer beginPage;
    private Integer endPage;
    
    public QueryResult(T resultList,
                       Integer resultRows,
                       Integer resultPages,
                       Integer currentPage) {
        this.resultList = resultList;
        this.resultRows = resultRows;
        this.resultPages = resultPages;
        this.currentPage = currentPage;
    }
    
    public QueryResult(T resultList,
                       Integer resultRows) {
        this.resultList = resultList;
        this.resultRows = resultRows;
    }
    
    public QueryResult(T resultList) {
        this.resultList = resultList;
    }
    
    public QueryResult() {
    }
    
    /**
     * @return Obter o atributo resultList.
     */
    public T getResultList() {
        return resultList;
    }
    
    /**
     * @param resultList Define o valor para o atributo resultList.
     */
    public void setResultList(T resultList) {
        this.resultList = resultList;
    }
    
    /**
     * @return Obter o atributo resultRows.
     */
    public Integer getResultRows() {
        if (this.resultRows == null && this.resultList != null && !this.resultList.isEmpty()) {
            this.resultRows = this.resultList.size();
        }
        return resultRows;
    }
    
    /**
     * @param resultRows Define o valor para o atributo resultRows.
     */
    public void setResultRows(Integer resultRows) {
        this.resultRows = resultRows;
    }
    
    /**
     * @return Obter o atributo resultPages.
     */
    public Integer getResultPages() {
        return resultPages;
    }
    
    /**
     * @param resultPages Define o valor para o atributo resultPages.
     */
    public void setResultPages(double resultPages) {
        BigDecimal decimal = new BigDecimal(resultPages).setScale(0, RoundingMode.UP);
        int absolute = decimal.intValue();
        this.resultPages =  absolute;
    }
    
    public static void main(String[] args) {
        
    }

    
    /**
     * @return Obter o atributo currentPage.
     */
    public Integer getCurrentPage() {
        return currentPage;
    }

    
    /**
     * @param currentPage Define o valor para o atributo currentPage.
     */
    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }
    
    /**
    *
    * @param resultsGroup
    * @param maxResultsPage
    * 
    * @author Maikon Rafael da Rocha
    * @since 17/06/2011 13:33:06
    */
   public void completeResultListPage(int maxResultsRow, int maxResultsPage) {
       if (this.resultList != null && this.resultList.size() > 0 && this.resultRows != null && this.resultRows > 0) {
           if (this.resultList.size() < maxResultsPage) {
               BigDecimal mod = new BigDecimal((this.resultList.size()*1.0) / (maxResultsRow*1.0)).setScale(0, RoundingMode.UP);
               int absolute = mod.intValue();
               int totalRows = absolute * maxResultsRow;
               for (int i = this.resultList.size(); i < totalRows; i++) {
                   this.resultList.add(null);
               }
           }
       }
   }

    /**
     * 
     * 
     * 
     * @author Maikon Rafael da Rocha
     * @since 17/06/2011 13:33:15
     */
    public void paginarComLimiteBusca() {
        if (this.currentPage != null && this.resultPages != null) {
            int pag = this.currentPage;
            int numPaginas = this.resultPages;
            int pagInicio = pag - 3;
            int pagFim = pag + 3;
            if (pagInicio < 1) {
                pagFim += (pagInicio * -1);
                pagInicio = 1;
            }
            if (pagFim > numPaginas) {
                pagInicio -= (pagFim - numPaginas);
                pagFim = numPaginas;
                if (pagInicio < 1)
                    pagInicio = 1;
            }
            this.beginPage = pagInicio;
            this.endPage = pagFim;
        }
    }

    
    /**
     * @return Obter o atributo beginPage.
     */
    public Integer getBeginPage() {
        return beginPage;
    }

    
    /**
     * @param beginPage Define o valor para o atributo beginPage.
     */
    public void setBeginPage(Integer beginPage) {
        this.beginPage = beginPage;
    }

    
    /**
     * @return Obter o atributo endPage.
     */
    public Integer getEndPage() {
        return endPage;
    }

    
    /**
     * @param endPage Define o valor para o atributo endPage.
     */
    public void setEndPage(Integer endPage) {
        this.endPage = endPage;
    }

}
